﻿using Sunny.UI;
using System;
using System.Windows.Forms;
using WCS.Utils;

namespace WCS.Controls.系统查询
{
    public partial class AddMaterial : UIForm
    {
        public AddMaterial()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();

        private void AddTask_Load(object sender, EventArgs e)
        {
        }

        private void AddBtn_Click(object sender, EventArgs e)
        {
            var matial = SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>()
                .Where(w => w.MaterialType.Contains("物料"))
                .First();

            if (matial == null)
            {
                MessageBox.Show("物料已经满了");
                return;
            }
            if (this.PalletCodeTXT.Text == "" || this.MaterialTypeTXT.Text == "")
            {
                MessageBox.Show("物料编号或者物料名称为空！");
                return;
            }
            if (this.PalletCodeTXT.Text.Length != 8)
            {
                MessageBox.Show("物料编号为八位数字！");
                return;
            }
            SqlSugarDapper.DB.Updateable<Entity.PalletToMaterial>()
                .SetColumns(w => w.PalletCode == PalletCodeTXT.Text)
                .SetColumns(w => w.MaterialType == MaterialTypeTXT.Text)
                .SetColumns(w => w.Flag =="true")
                .Where(w => w.MaterialType == matial.MaterialType)
                .ExecuteCommand();

            MessageBox.Show("添加物料成功!");
            this.PalletCodeTXT.Text = null;
            this.MaterialTypeTXT.Text = null;
            this.Close();
            PalletToMaterial._PalletToMaterial.Re();
        }
    }
}
